<h2>Why is this an issue?</h2>
<p>Calling an overridable method from a constructor could result in failures or strange behaviors when instantiating a subclass which overrides the
method.</p>
<p>When constructing an object of a derived class, the constructor of the parent class is invoked first, and only then the constructor of the derived
class is called. This sequential construction process applies to multiple levels of inheritance as well, starting from the base class and progressing
to the most derived class.</p>
<p>If an overridable method is called within the constructor of the parent class, it may inadvertently invoke an overridden implementation in the
derived class. This can lead to unexpected failures or strange behaviors because the object’s construction is still in progress and may not have
reached a fully initialized state. Consequently, the overridden method may rely on uninitialized members or have assumptions about the object’s state
that are not yet valid.</p>
<p>For example:</p>
<pre>
public class Parent
{
  public Parent()
  {
    DoSomething();  // Noncompliant
  }

  public virtual void DoSomething() // can be overridden
  {
    // ...
  }
}

public class Child : Parent
{
  private string foo;

  public Child(string foo) // leads to call DoSomething() in Parent constructor which triggers a NullReferenceException as foo has not yet been initialized
  {
    this.foo = foo;
  }

  public override void DoSomething()
  {
    Console.WriteLine(this.foo.Length);
  }
}
</pre>
<ul>
  <li> The <code>Child</code> class constructor starts by calling the <code>Parent</code> class constructor. </li>
  <li> The <code>Parent</code> class constructor calls the method <code>DoSomething</code>, which has been overridden in the <code>Child</code> class.
  </li>
  <li> If the behavior of the <code>Child</code> class overridden <code>DoSomething</code> method depends on fields that are initialized in the
  <code>Child</code> class constructor, unexpected behavior (such as a <code>NullReferenceException</code>) can result, because the fields aren’t
  initialized yet. </li>
</ul>
<h2>How to fix it</h2>
<p>Depending on the context, you can either:</p>
<ul>
  <li> avoid calling overridable methods from constructors. This is the recommended approach </li>
  <li> ensure that the method is not overridden in any derived classes. This can be done by marking the method as <code>sealed</code> in the current
  class </li>
  <li> ensure that the class is not inherited from. This can be done by marking the class as <code>sealed</code> </li>
</ul>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
class Parent
{
  public virtual void DoSomething() { }
}

class Child : Parent
{
  public Child()
  {
    DoSomething();  // Noncompliant
  }
}
</pre>
<pre data-diff-id="2" data-diff-type="noncompliant">
class Parent
{
  public virtual void DoSomething() { }
}

class Child : Parent
{
  public Child()
  {
    DoSomething();  // Noncompliant
  }
}
</pre>
<pre data-diff-id="3" data-diff-type="noncompliant">
class Parent
{
  public virtual void DoSomething() { }
}

class Child : Parent
{
  public Child()
  {
    DoSomething();  // Noncompliant
  }
}
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
class Parent
{
  public virtual void DoSomething() { }
}

class Child : Parent
{
  public Child()
  {
    // Call removed
  }
}
</pre>
<pre data-diff-id="2" data-diff-type="compliant">
class Parent
{
  public virtual void DoSomething() { }
}

class Child : Parent
{
  public Child()
  {
    DoSomething();
  }

  // Method sealed to prevent overriding
  public sealed override void DoSomething()
  {
    base.DoSomething();
  }
}
</pre>
<pre data-diff-id="3" data-diff-type="compliant">
class Parent
{
  public virtual void DoSomething() { }
}

// Class sealed to prevent inheritance
sealed class Child : Parent
{
  public Child()
  {
    DoSomething();
  }
}
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
  <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors">Constructors</a>
  </li>
  <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/object-oriented/inheritance">Inheritance</a> </li>
  <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/object-oriented/polymorphism">Polimorphism</a> </li>
  <li> Microsoft Learn - <a
  href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods#method-signatures">Methods - Method
  signatures</a> </li>
  <li> Stack Overflow - Answer by Eric Lippert for <a href="https://stackoverflow.com/a/20418640">Overriding and calling same method in Base class
  constructor in C#</a> </li>
  <li> Fabulous adventures in coding - <a
  href="https://ericlippert.com/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one">Why Do Initializers Run In The
  Opposite Order As Constructors?</a> </li>
</ul>

